package code;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;

import common.ClampFactory;

public class CodeDAO {
	private SqlSession session;
	CodeDTO dto = null;
	String srchWord = "";
	
	public String getSrchWord() {
		return srchWord;
	}
	public void setSrchWord(String srchWord) {
		this.srchWord = srchWord;
	}
	public CodeDTO getDto() {
		return dto;
	}
	public void setDto(CodeDTO dto) {
		this.dto = dto;
	}
	
	public CodeDAO() {
		try {
			session = ClampFactory.getFactory().openSession();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * code 리스트
	 * @return codeList
	 */
	public ArrayList<CodeDTO> list(){
		ArrayList<CodeDTO> codeList = null;
		try {
			codeList = (ArrayList<CodeDTO>)session.selectList("code.list");
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return codeList;
	}
	
	/**
	 * code 검색 결과 리스트
	 * @return srchList
	 */
	public ArrayList<CodeDTO> cdSrch(String srchWord){
		ArrayList<CodeDTO> srchList = new ArrayList<CodeDTO>();
		try {
			//System.out.println("dao.cdSrch() : "+srchWord);
			srchList = (ArrayList<CodeDTO>)session.selectList("code.cdSrch", srchWord);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return srchList;
	}
	
	/**
	 * grp2 자동 채번
	 * @return grp2
	 */
	public String grp2(){
		String grp2 = "";
		try {
			grp2 = (String)session.selectOne("grp2_inc", dto);
			//System.out.println("CD_GRP2 : "+grp2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return grp2;
	}
	/**
	 * code 등록
	 * @return yn
	 */
	public int insert(){
		int yn = 0;
		try {
			yn = session.insert("code.insert", dto);
			//System.out.println("xml갔다왔지롱 insert됐나~? : "+yn);
			session.commit();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return yn;
	}
	
	/**
	 * CODE 수정
	 * @return
	 */
	public int update(){
		int yn = 0;
		try {
			yn = session.update("code.update", dto);
			session.commit();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return yn;
	}
	
	public CodeDTO selOneDto(){
		CodeDTO selOne = new CodeDTO();
		try {
			selOne = (CodeDTO)session.selectOne("code.selOneDto", dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return selOne;
	}
	/**
	 * 공통 메소드 (은규 요청) 2014.08.08
	 * @return ArrayList<CodeDTO> codes
	 */
	public ArrayList<CodeDTO> returnCode(){
		ArrayList<CodeDTO> codes = new ArrayList<CodeDTO>();
		try {
			codes = (ArrayList<CodeDTO>)session.selectList("code.selCode", dto);
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return codes;
	}
}
